package com.songcha.module_home.ui.activity.search

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.songcha.library_base.mvvm.base.BaseMvvmActivity
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.interfaces.IOnBookClickListener
import com.songcha.library_business.proxy.database.BookSearchHistoryDaoManager
import com.songcha.library_business.ui.adapter.BookListAdapter
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_database_douyue.bean.BookSearchHistoryBean
import com.songcha.module_home.R
import com.songcha.module_home.databinding.HomeActivitySearchBinding
import com.songcha.module_home.ui.adapter.SearchHistoryAdapter

@Route(path = "/home/SearchActivity")
class SearchActivity:BaseMvvmActivity<SearchViewModel,HomeActivitySearchBinding>(),OnItemChildClickListener,IOnBookClickListener {
    private var mSearchHistoryAdapter:SearchHistoryAdapter?=null
    private var mSearchResultAdapter:BookListAdapter?=null

    override fun getLayoutId(): Int {
        return R.layout.home_activity_search
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        viewModel.getSearchHistory()

        databinding.homeBtnSearch.setOnClickListener {
            searchBook()
        }

        databinding.homeBtnDelHistory.setOnClickListener {
            showDelHistoryDialog()
        }

        viewModel.searchHistoryList.observe(this){data->
            if(data.size>0){
                databinding.homeBtnDelHistory.visibility=View.VISIBLE
            }else{
                databinding.homeBtnDelHistory.visibility=View.GONE
            }
            initSearchHistoryRcv(data)
        }

        viewModel.searchList.observe(this){data->
            initSearchResultRcv(data)
           /* if(data.size>0){
                databinding.homeTvNodata.visibility=View.GONE
            }else{
                databinding.homeTvNodata.visibility=View.VISIBLE
            }*/
        }
    }

    override fun isImmerseStatusBar(): Boolean {
        return true
    }

    /*override fun isShowHeaderBar(): Boolean {
        return true
    }*/

    private fun initSearchResultRcv(list:MutableList<BookNetBean>){
        databinding.homeRcvSearchResult.apply {
            layoutManager=LinearLayoutManager(context)
            mSearchResultAdapter=BookListAdapter(list,this@SearchActivity,false)
            adapter=mSearchResultAdapter
        }
    }

    override fun getLoadNoDataLayoutId(): Int {
        return R.layout.home_stub_search_nodata
    }

    /*override fun isShowHeaderBar(): Boolean {
        return true
    }*/

    override fun getLoadViewTopMargin(): Int {
        if(isViewModelInitialized()){
            val point=IntArray(2)
            databinding.homeRcvSearchHistory.getLocationInWindow(point)
            return point[1]+databinding.homeRcvSearchHistory.height
        } else
            return 0
    }

    override fun getLoadNoDataTip(): String {
        return "暂未搜到您想要的内容"
    }

    private fun showDelHistoryDialog(){
        val db=CustomDialog.Companion.Builder(this)
        db.setDialogMsg("是否删除搜索记录？")
            .setOnConfirmListener {
                BookSearchHistoryDaoManager.delAllSearchHistory()
                viewModel.getSearchHistory()
                true
            }
            .create().show()
    }

    private fun initSearchHistoryRcv(list:MutableList<BookSearchHistoryBean>){
       /* if(list.size==0){
            databinding.homeRlSearchHistory.visibility=View.GONE
        }else{
            databinding.homeRlSearchHistory.visibility=View.VISIBLE
        }*/
        databinding.homeRcvSearchHistory.apply {
            val lm=FlexboxLayoutManager(context)
            lm.flexDirection=FlexDirection.ROW
            lm.flexWrap=FlexWrap.WRAP
            lm.justifyContent=JustifyContent.FLEX_START
            layoutManager=lm
            mSearchHistoryAdapter=SearchHistoryAdapter(list)
            mSearchHistoryAdapter!!.setOnItemChildClickListener(this@SearchActivity)
            adapter=mSearchHistoryAdapter

        }
    }

    private fun searchBook(){
        val key=databinding.homeEdtSearch.text.toString().trim()
        if(key.isBlank()){
            ToastUtil.show("请输入搜索关键字")
            return
        }
        viewModel.searchBook(key)
        viewModel.getSearchHistory()
    }

    override fun onRetry() {
        searchBook()
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when(view.id){
            R.id.home_item_btn_search_history->{
                view as CustomButton
                databinding.homeEdtSearch.setText(view.text.toString())
            }
        }
    }

    override fun onBookClick(bookId: String,img: ImageView) {
        BusinessJumpUtil.toBookDetail(bookId)
    }

}